package view.component.effects
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import framework.core.AssetsManager;
	import framework.view.components.graphics.SWF;
	
	import mapengine.effects.EffectObjectBase;
	import mapengine.effects.IEffectAsset;
	
	public class BattleSkillView extends BattleTextFieldView implements IEffectAsset
	{
		public function BattleSkillView()
		{
			super();
		}
		
		override protected function init():void
		{
			
		}
		
		private var _text:String;
		
		override public function set text(value:String):void
		{
			if(skin)
			{
				if(skin.label) skin.label.text = value;
			}
			else
			{
				_text = value;
			}
		}
		
		protected var swf:SWF;
		
		protected function loadAsset(url:String, callBack:Function, mode:String = "externalLabs"):void
		{
			if (swf)
			{
				swf.dispose();
				swf = null;
			}
			swf = AssetsManager.loadAsset(url, callBack, null, mode) as SWF;
		}
		
		private var _classRefStr:String;
		
		public function initlize(path:String , classRefStr:String):void
		{
			_classRefStr = classRefStr;
			loadAsset(path, assetLoadedHandler);
		}
		
		protected function assetLoadedHandler(swf:SWF):void
		{
			var classRef:Class = swf.getClassRef(_classRefStr);
			skin = new classRef() as MovieClip;
			this.addChild(skin);
			if(_start)
			{
				realStart();
			}
			
			text = _text;
		}
		
		private function realStart():void
		{
			skin.addEventListener("effect_complete" , onEffectComplete);
			skin.gotoAndPlay("effect");
		}
		
		public function start():void
		{
			if(skin)
			{
				realStart();
			}
			else
			{
				_start = true;
			}
		}
		
		private var _start:Boolean = false;
		
		protected function onEffectComplete(evt:Event):void
		{
			skin.removeEventListener("effect_complete" , onEffectComplete);
			dispose();
		}
		
		override public function dispose():void
		{
			if(skin)
			{
				skin.removeEventListener("effect_complete" , onEffectComplete);
			}
			super.dispose();
		}
	}
}